package cn.uin.domain.shared.adapter.repository;

/**
 * @ClassName SimplePageRequest
 * @Description:简单分页请求实现类(实现Pageable接口，提供基本的分页参数封装)
 * @Author: Administrator
 * @CreateDate: 2025/11/14 17:11
 * @Version: 1.0
 */
public class SimplePageRequest implements IPageable {

	private final int page;
	private final int size;
	private final ISort ISort;

	/**
	 * 构造函数
	 * @param page 页码（从0开始）
	 * @param size 页大小
	 * @param ISort 排序对象
	 * @throws IllegalArgumentException 如果页码小于0或页大小小于1
	 */
	public SimplePageRequest(int page, int size, ISort ISort) {
		if (page < 0) {
			throw new IllegalArgumentException("Page index must not be less than zero");
		}
		if (size < 1) {
			throw new IllegalArgumentException("Page size must not be less than one");
		}

		this.page = page;
		this.size = size;
		this.ISort = ISort != null ? ISort : ISort.unsorted();
	}

	@Override
	public int getPageNumber() {
		return page;
	}

	@Override
	public int getPageSize() {
		return size;
	}

	@Override
	public long getOffset() {
		return (long) page * size;
	}

	@Override
	public ISort getSort() {
		return ISort;
	}

	@Override
	public IPageable next() {
		return new SimplePageRequest(page + 1, size, ISort);
	}

	@Override
	public IPageable previousOrFirst() {
		return hasPrevious() ? new SimplePageRequest(page - 1, size, ISort) : first();
	}

	@Override
	public IPageable first() {
		return new SimplePageRequest(0, size, ISort);
	}

	@Override
	public boolean hasPrevious() {
		return page > 0;
	}

	@Override
	public boolean isPaged() {
		// 明确表示启用分页
		return true;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (!(obj instanceof SimplePageRequest)) return false;
		SimplePageRequest that = (SimplePageRequest) obj;
		return page == that.page && size == that.size && ISort.equals(that.ISort);
	}

	@Override
	public int hashCode() {
		int result = page;
		result = 31 * result + size;
		result = 31 * result + ISort.hashCode();
		return result;
	}

	@Override
	public String toString() {
		return String.format("Page request [number: %d, size: %d, sort: %s]", page, size, ISort);
	}
}
